﻿@page "/screen"
@layout EmptyLayout
@using Microsoft.JSInterop;
@using TMV.Application.AbnormalRecords.Services;
@using TMV.Application.Tr.Services;
@using TMV.Application.Home.Services;
@using TMV.DTO.AbnormalRecords;
@using TMV.DTO.Tr;
@using TMV.DTO.Home;
@using TMV.DTO;
@using TMV.Web.Pages.AbnormalRecords.ViewModel;
@using TMV.Web.Pages.TransportationRecords.ViewModel;
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager Navigation
@implements IAsyncDisposable
@inherits LayComponentBase
@inject IJSRuntime Js;
@inject ITrServiceDM _trRecodeServiceDM
@inject IArServiceDM _arServiceDM
@inject IHomeServiceDM _screen
@attribute [AllowAnonymous]


<div style="background:#F0F3FA !important;height:100%;width:100%;padding:8px;overflow:hidden;">
    <MRow Style="height:400px;">
        <MCol Md=12 Sm=12>
            <MCard Class="max-height">
                <MCardTitle Style="padding:4px 16px;"><MIcon Left>mdi-audio-video</MIcon><span style="font-size:16px;">运输动态</span></MCardTitle>
                <MDataTable Headers="_headers" Items=" tpPage.Datas" HideDefaultFooter Class="table-border-none pt-2">
                    <HeaderColContent Context="header">
                        <span class="text-subtitle">@header.Text</span>
                    </HeaderColContent>
                    <ItemColContent>
                        @if (context.Header.Value == nameof(TransportationRecordsDTO.Huanjie))
                        {
                            @if (context.Item.Huanjie == 1)
                            {
                                <MChip Color="@($"info-lighten-1")" TextColor="info" Ripple="false">
                                    <span>未上传</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 2)
                            {
                                <MChip Color="@($"info-lighten-2")" TextColor="info" Ripple="false">
                                    <span>进场</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 3)
                            {
                                <MChip Color="@($"info-lighten-3")" TextColor="info" Ripple="false">
                                    <span>采样</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 4)
                            {
                                <MChip Color="@($"info-lighten-4")" TextColor="info" Ripple="false">
                                    <span>毛重</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 5)
                            {
                                <MChip Color="@($"info-lighten-5")" TextColor="info" Ripple="false">
                                    <span>卸煤</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 6)
                            {
                                <MChip Color="@($"info-lighten-6")" TextColor="info" Ripple="false">
                                    <span>皮重</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 7)
                            {
                                <MChip Color="@($"info-lighten-7")" TextColor="info" Ripple="false">
                                    <span>打印/上传</span>
                                </MChip>
                            }
                            else if (context.Item.Huanjie == 8)
                            {
                                <MChip Color="@($"info-lighten-8")" TextColor="info" Ripple="false">
                                    <span>出厂</span>
                                </MChip>
                            }
                            else
                            {
                                <span>无</span>
                            }
                        }
                        else
                        {
                            @context.Value
                        }
                    </ItemColContent>
                </MDataTable>
            </MCard>
        </MCol>
    </MRow>
    <MRow Style="height:calc(100% - 366px);margin-top:0;">
        <MCol Md=8 Sm=12 Class="max-height" Style="padding-right:0;">
            <MCard Class="max-height">
                <MCardTitle Style="padding:4px 16px;"><MIcon Left>mdi-bell-ring</MIcon> <span style="font-size:16px;">告警动态</span></MCardTitle>
                <MDataTable Headers="_saheaders" Items="abRePage" HideDefaultFooter Class="table-border-none pt-2">
                    <HeaderColContent Context="header">
                        <span class="text-subtitle">@header.Text</span>
                    </HeaderColContent>
                </MDataTable>
            </MCard>
        </MCol>
        <MCol Md=4 Sm=12 Class="max-height">
            <MCard Class="max-height">
                <MCardTitle Style="padding:4px 16px;"><MIcon Left>mdi-chart-pie</MIcon> <span style="font-size:16px;">今日统计</span></MCardTitle>
                <MCardText Class="pb-0">
                    <MRow NoGutters>
                        <MCol Md=6 Sm=12 Class="py-12">
                            <div class="d-flex block-center">
                                <MAvatar Size=56 Color="fill-lighten-1">
                                    <MIcon Color="sample-green" Size=32>mdi-weight-kilogram</MIcon>
                                </MAvatar>
                                <div class="ml-3">
                                    <div class="text-btn neutral-lighten-4--text">净重总量</div>
                                    <h6>@countDTO?.Jzzl &nbsp;<span style="font-size:12px;color:#2c2c2c;">(吨)</span></h6>
                                </div>
                            </div>
                        </MCol>
                        <MCol Md=6 Sm=12 Class="py-12">
                            <div class="d-flex block-center">
                                <MAvatar Size=56 Color="fill-lighten-1">
                                    <MIcon Color="dark-yellow" Size=32>mdi-car-lifted-pickup</MIcon>
                                </MAvatar>
                                <div class="ml-3">
                                    <div class="text-btn neutral-lighten-4--text">今日车次</div>
                                    <h6>@countDTO?.Jrcc &nbsp;<span style="font-size:12px;color:#2c2c2c;">(次)</span></h6>
                                </div>
                            </div>
                        </MCol>
                        <MCol Md=6 Sm=12 Class="py-12">
                            <div class="d-flex block-center">
                                <MAvatar Size=56 Color="fill-lighten-1">
                                    <MIcon Color="error" Size=32>mdi-bell-alert</MIcon>
                                </MAvatar>
                                <div class="ml-3">
                                    <div class="text-btn neutral-lighten-4--text">告警次数</div>
                                    <h6>@countDTO?.Gjcs &nbsp;<span style="font-size:12px;color:#2c2c2c;">(次)</span></h6>
                                </div>
                            </div>
                        </MCol>
                        <MCol Md=6 Sm=12 Class="py-12">
                            <div class="d-flex block-center">
                                <MAvatar Size=56 Color="fill-lighten-1">
                                    <MIcon Size=32>mdi-grill-outline</MIcon>
                                </MAvatar>
                                <div class="ml-3">
                                    <div class="text-btn neutral-lighten-4--text">今日矿数</div>
                                    <h6>@countDTO?.Jrks &nbsp;<span style="font-size:12px;color:#2c2c2c;">(个)</span></h6>
                                </div>
                            </div>
                        </MCol>
                    </MRow>
                </MCardText>
            </MCard>
        </MCol>
    </MRow>
</div>


@code {
    public TransportationRecordsPage tpPage = new();
    public List<ResultInfo> abRePage = new List<ResultInfo>();
    public HomeCountDTO countDTO = new();

    private HubConnection? hubConnection;
    List<DataTableHeader<TransportationRecordsDTO>> _headers = new()
    {
            new() { Text = "物流单号", Value = nameof(TransportationRecordsDTO.Code), CellClass = "" },
            new() { Text = "车牌号", Value = nameof(TransportationRecordsDTO.PlateNumber)},
            new() { Text = "矿号", Value = nameof(TransportationRecordsDTO.MineCode) },
            new() { Text = "毛重(t)", Value = nameof(TransportationRecordsDTO.RoughWeight) },
            new() { Text = "皮重(t)", Value = nameof(TransportationRecordsDTO.TareWeight) },
            new() { Text = "净重(t)", Value = nameof(TransportationRecordsDTO.NetWeight) },
            new() { Text = "进厂时间", Value = nameof(TransportationRecordsDTO.STime) },
            new() { Text = "出厂时间", Value = nameof(TransportationRecordsDTO.ETime) },
            new() { Text = "当前环节", Value = nameof(TransportationRecordsDTO.Huanjie) }
    };
    List<DataTableHeader<ResultInfo>> _saheaders = new()
    {
         new() { Text = "错误码", Value = nameof(ResultInfo.Error) },
         new() { Text = "提醒内容", Value = nameof(ResultInfo.Message), CellClass = "redText" },

    };

    private async Task Speak(string? word)
    {
        await Js.InvokeVoidAsync("speak", word);
    }

    private void UpdateOrder(TransportationRecordsDTO dto)
    {
        var index = tpPage.Datas.FindIndex(x => x.Id == dto.Id);
        if (index != -1)
        {
            tpPage.Datas[index] = dto;
        }
        else
        {
            tpPage.Datas.Insert(0, dto);
        }
        AbReInitialize();
    }

    protected override async Task OnInitializedAsync()
    {
        hubConnection = new HubConnectionBuilder()
            .WithUrl(Navigation.ToAbsoluteUri("/chathub"))
            .Build();

        hubConnection.On<string, string>("ReceiveMessage", async (user, message) =>
        {
            if (user == "warning")
            {
                var info = JsonConvert.DeserializeObject<ResultInfo>(message);
                if (info != null)
                {
                    if (string.IsNullOrEmpty(info.Message))
                    {
                        await Speak(info?.Message);
                        abRePage.Insert(0, info);
                    }
                }
            }
            else if (user == "record")
            {
                var record = JsonConvert.DeserializeObject<TransportationRecordsDTO>(message);
                if (record != null)
                {
                    UpdateOrder(record);
                }
            }
            await InvokeAsync(StateHasChanged);
        });

        await hubConnection.StartAsync();
    }

    public bool IsConnected =>
        hubConnection?.State == HubConnectionState.Connected;

    public async ValueTask DisposeAsync()
    {
        if (hubConnection is not null)
        {
            await hubConnection.DisposeAsync();
        }
    }

    protected override void OnInitialized()
    {
        Initialize();
        AbReInitialize();
    }
    public void Initialize()
    {
        var rest = _screen.GetHomeTransportationRecordsList();
        tpPage = new TransportationRecordsPage(rest.Data) { };
    }
    public void AbReInitialize()
    {
        countDTO = _screen.GetHomeCount().Data;
    }
}
